package cn.autoOffice.service.impl;

import cn.autoOffice.bean.dto.EmployeeDto;
import cn.autoOffice.bean.pojo.Employee;
import cn.autoOffice.mapper.EmpMapper;
import cn.autoOffice.service.EmpService;
import cn.autoOffice.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 金健伟
 * @DATE 2023/3/27 10:41
 * 功能说明：
 */
@Service
public class EmpServiceImpl implements EmpService {

//    @Autowired
    @Resource
    private EmpMapper empMapper;

    @Autowired
    private RedisService redisService;

    private static final String EMP_CODE_PREFIX = "empCode";

    public Integer countEmp(){
        String count = redisService.getValue("allEmpCount");
        if (count != null && !"".equals(count)){
            return Integer.parseInt(count);
        } else {
            Integer integer = empMapper.countEmp();
            redisService.putValue("allEmpCount",integer);
            return integer;
        }
    }

    public List<Employee> getEmployeeInfoByCode(String empCode){
        List<Employee> employeeList;
        // if (empCode == null || empCode.isEmpty()){
        //     String value = redisService.getValue("empCodeAll");
        //     if (value != null && !value.isEmpty()){
        //         return JSONObject.parseArray(value, Employee.class);
        //     }
        //     employeeList = empMapper.getEmployeeInfoByCode(empCode);
        //     if (employeeList != null) {
        //         redisService.putValue("empCodeAll", employeeList, 10);
        //     }
        //     return employeeList;
        // } else {
        //     String key = EMP_CODE_PREFIX + empCode;
        //     String value = redisService.getValue(key);
        //     if (value != null && !value.isEmpty()){
        //         return JSONObject.parseArray(value, Employee.class);
        //     }
            employeeList = empMapper.getEmployeeInfoByCode(empCode);
            // if (employeeList != null) {
            //     redisService.putValue(key,employeeList,10);
            // }
            return employeeList;
        // }
    }

    public int addEmployee(Employee employee) {
        return empMapper.addEmployee(employee);
    }
    public int updateEmployee(Employee employee) {
        return empMapper.updateEmployee(employee);
    }
    public int deleteEmployee(int id) {
        return empMapper.deleteEmployee(id);
    }

    public EmployeeDto getFullEmpRoleName(Integer empCode){
        return empMapper.getFullEmpRoleName(empCode);
    }

}
